import 'package:f_enjoy_plus/components/TabBar.dart';
import 'package:f_enjoy_plus/core/theme/app_theme.dart';
import 'package:f_enjoy_plus/pages/home/components/NoticeDetail.dart';
import 'package:f_enjoy_plus/pages/house/addHouse.dart';
import 'package:f_enjoy_plus/pages/house/houseDetail.dart';
import 'package:f_enjoy_plus/pages/house/index.dart';
import 'package:f_enjoy_plus/pages/house/location.dart';
import 'package:f_enjoy_plus/pages/login/index.dart';
import 'package:f_enjoy_plus/pages/profile/index.dart';
import 'package:f_enjoy_plus/pages/repair/addOrEdit.dart';
import 'package:f_enjoy_plus/pages/repair/myRepairPage.dart';
import 'package:f_enjoy_plus/utils/RouteObserver.dart';
import 'package:flutter/material.dart';
import 'package:f_enjoy_plus/utils/loading.dart';
import 'package:get/get.dart';
import 'utils/token.dart';

void main() {
  runApp(
    GetMaterialApp(
      title: 'EnjoyPlus',
      theme: appTheme,
      navigatorKey: LoadingUtils.navigatorKey,
      navigatorObservers: [routeObserver], // 添加观察者
      initialRoute: '/',
      getPages: [
        GetPage(name: '/', page: () => const TabBarComp()),
        GetPage(name: '/login', page: () => const LoginPage()),
        GetPage(name: '/repair', page: () => RepairAddOrEditPage(), middlewares: [AuthMiddleware()]),
        GetPage(name: '/house', page: () => HouseList(), middlewares: [AuthMiddleware()]), // 保护的路由
        GetPage(name: '/location', page: () => Location(), middlewares: [AuthMiddleware()]), // 保护的路由
        GetPage(name: '/addHouse', page: () => AddHouse(routerParams: Get.arguments), middlewares: [AuthMiddleware()]), // 保护的路由
        GetPage(name: '/houseDetail', page: () => HouseDetail(routerParams: Get.arguments), middlewares: [AuthMiddleware()]), // 保护的路由
        GetPage(name: '/noticeDetail', page: () => const NoticeDetail(), middlewares: [AuthMiddleware()]), // 保护的路由
        GetPage(name: '/profile', page: () => ProfilePage(userInfo: Get.arguments), middlewares: [AuthMiddleware()]), // 保护的路由
        GetPage(name: '/myRepair', page: () => MyrepairPage(), middlewares: [AuthMiddleware()]),
      ],
    //   routes: {
    //     '/': (context) => const TabBarComp(),
    //     '/login': (context) => const LoginPage(),
    //   },
    //   onGenerateRoute: (settings) {
    //     final token = TokenManager.instance.getToken() ?? '';
    //     print('token: $token');
    //     if (token.isEmpty || settings.name == '/login') {
    //       return MaterialPageRoute(builder: (context) => const LoginPage());
    //     }

    //     switch (settings.name) {
    //       case '/noticeDetail':
    //         return MaterialPageRoute(
    //           builder: (context) => const NoticeDetail(),
    //           settings: settings,
    //         );
    //       case '/profile':
    //         return MaterialPageRoute(
    //           builder:
    //               (context) => ProfilePage(userInfo: settings.arguments as Map),
    //           settings: settings,
    //         );
    //       case '/house':
    //         return MaterialPageRoute(builder: (context) => HouseList());
    //       case '/location':
    //         return MaterialPageRoute(builder: (context) => Location());
    //       case '/addHouse':
    //         return MaterialPageRoute(
    //           builder:
    //               (context) =>
    //                   AddHouse(routerParams: settings.arguments as Map),
    //         );
    //       case '/houseDetail':
    //         return MaterialPageRoute(
    //           builder:
    //               (context) =>
    //                   HouseDetail(routerParams: settings.arguments as Map),
    //           settings: settings,
    //         );
    //     }
    //     return null;
    //   },
    ),
  );
}

class AuthMiddleware extends GetMiddleware {
  @override
  RouteSettings? redirect(String? route) {
    final token = TokenManager.instance.getToken() ?? '';
    if (token.isEmpty && route != '/login') {
      return const RouteSettings(name: '/login');
    }
    return null; // 允许访问
  }
}
